package org.lewis.demo.controller;

import com.fasterxml.jackson.databind.JsonNode;
import org.lewis.demo.util.WebClientUtil;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("demo")
@RestController
public class DemoController {

    private final WebClientUtil util;

    public DemoController(WebClientUtil util) {
        this.util = util;
    }

    @GetMapping("userInfo/{username}")
    public JsonNode userInfo(@PathVariable String username) {
        return this.util.userInfo(username);
    }

    @GetMapping(value = "avatar/{username}", produces = MediaType.IMAGE_PNG_VALUE)
    public byte[] avatar(@PathVariable String username) {
        return this.util.avatar(username);
    }

    @GetMapping("events/{username}")
    public JsonNode events(@PathVariable String username) {
        return this.util.publicEvents(username);
    }

    @GetMapping("repos/{username}")
    public List<JsonNode> repos(@PathVariable String username) {
        return this.util.publicRepos(username)
                .stream().map(jsonNode -> jsonNode.path("full_name"))
                .collect(Collectors.toList());
    }

}
